﻿#if !(NETFX_CORE || WINDOWS_PHONE)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.Security.Principal;
using System.Security.AccessControl;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;

namespace WindowsCommonStorage
{
    internal class NetFxApplicatonData:CommonAppllicationData
    {
        protected internal override CommonStorageFolder ProtectedLocalFolder
        {
            get { return localFolder; }
        }
        private CommonStorageFolder localFolder;

        protected internal override CommonStorageFolder ProtectedRomingFolder
        {
            get { return romingFolder; }
        }
        private CommonStorageFolder romingFolder;

        protected internal override CommonStorageFolder ProtectedTemporaryFolder
        {
            get { return temporaryFolder; }
        }
        private CommonStorageFolder temporaryFolder;

        protected internal override async Task ProtectedClearAsync()
        {
            await this.LocalFolder.DeleteAsync().ConfigureAwait(false);
            await this.RomingFolder.DeleteAsync().ConfigureAwait(false);
            await this.RomingFolder.DeleteAsync().ConfigureAwait(false);
        }

        internal NetFxApplicatonData(Assembly assembly)
        {
            
            SHA1Managed enc = new SHA1Managed();
            string keystr = "WINDOWSCOMMONSTORAGE_KEY";
            
            StringBuilder currentApplicationFolderKeyBuilder = new StringBuilder();
           
            GuidAttribute appAttribute = (GuidAttribute)Attribute.GetCustomAttribute(assembly, typeof(GuidAttribute));
            string currentApplicationName = keystr + "_" + (appAttribute == null ? "_DEFAULT_KEY" : appAttribute.Value);
            foreach (var byteapp in enc.ComputeHash(Encoding.UTF8.GetBytes(currentApplicationName)))
            {
                currentApplicationFolderKeyBuilder.AppendFormat("{0:X2}", byteapp);
            }

            string windowsStorageData = "\\WindowsStorageData\\";
            string currentApplicationFolderKey = assembly.GetName().Name +"_" + currentApplicationFolderKeyBuilder.ToString();
            string romingPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + windowsStorageData + currentApplicationFolderKey;
            string localPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + windowsStorageData + currentApplicationFolderKey;
            string tempPath = Path.GetTempPath() + windowsStorageData + currentApplicationFolderKey;
            var romingtask = Task.Factory.StartNew(() =>
            {
                romingFolder = new NetFxStorageFolder { _folder = Directory.Exists(romingPath) ? new DirectoryInfo(romingPath) : Directory.CreateDirectory(romingPath) };
            });
            var localtask = Task.Factory.StartNew(() =>
            {
                localFolder = new NetFxStorageFolder { _folder = Directory.Exists(romingPath) ? new DirectoryInfo(localPath) : Directory.CreateDirectory(localPath) };
            });

            var temptask = Task.Factory.StartNew(() =>
            {
                temporaryFolder = new NetFxStorageFolder { _folder = Directory.Exists(tempPath) ? new DirectoryInfo(tempPath) : Directory.CreateDirectory(tempPath) };
            });

            Task.WaitAll(new Task[]{ romingtask, localtask, temptask });
            

        }

        internal static readonly NetFxApplicatonData _current = new NetFxApplicatonData(Assembly.GetEntryAssembly());
    }
}
#endif